﻿using AutoMapper;
using HealthcareSystem.API.Write.Application.Command.ChronicDisease;
using HealthcareSystem.Domain.ChronicDiseaseManagement;
using MediatR;
using WeekExam.PassPort.ErrorCode;
using WeekExam.PassPort.Infrastructure.Interface;

namespace HealthcareSystem.API.Write.Application.CommandHandel.ChronicDisease
{
    public class HealthReportUpdateStatuCommandHandler : IRequestHandler<HealthReportUpdateStatuCommand, ApiResult<int>>
    {

        private readonly IBaseRepository<HealthReport> hrRepository;
        private readonly IMapper mapper;

        public HealthReportUpdateStatuCommandHandler(IBaseRepository<HealthReport> hrRepository, IMapper mapper)
        {
            this.hrRepository = hrRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(HealthReportUpdateStatuCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ResultCode.修改成功;
            result.Message = ResultCode.修改成功.ToString();

            var list = hrRepository.GetAll().FirstOrDefault(x=>x.ReportId == request.ReportId);
            if (list.ReleaseStatus == false)
            {
                list.ReleaseStatus = true;
            }
            else
            {
                list.ReleaseStatus = false;
            }
            var entity = await hrRepository.UpdAsync(list);
            result.Data = entity;
            return await Task.FromResult(result);
        }
    }
}
